package io.github.talelin.latticy.service.impl.ag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.talelin.latticy.dto.ag.monitoringroom.CreateOrUpdateMonitoringRoomDTO;
import io.github.talelin.latticy.mapper.ag.MonitoringRoomMapper;
import io.github.talelin.latticy.model.ag.MonitoringRoomDO;
import io.github.talelin.latticy.service.ag.MonitoringRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MonitoringRoomServiceImpl implements MonitoringRoomService {
    @Autowired
    MonitoringRoomMapper monitoringRoomMapper;

    @Override
    public IPage<MonitoringRoomDO> getMonitoringByPage(Page<MonitoringRoomDO> pager) {
        QueryWrapper<MonitoringRoomDO> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("update_time");
        return monitoringRoomMapper.selectPage(pager, wrapper);
    }

    @Override
    public IPage<MonitoringRoomDO> searchMonitoringByKeyword(Page<MonitoringRoomDO> pager, String keyword) {
        QueryWrapper<MonitoringRoomDO> wrapper = new QueryWrapper<>();
        wrapper.like("mr_name",keyword).or()
                .like("mr_state", keyword);
        return monitoringRoomMapper.selectPage(pager, wrapper);
    }

    @Override
    public MonitoringRoomDO getById(int id) {
        return monitoringRoomMapper.selectById(id);
    }

    @Override
    public List<MonitoringRoomDO> getMonitoringRooms() {
        return monitoringRoomMapper.selectList(null);
    }

    @Override
    public boolean createRoom(CreateOrUpdateMonitoringRoomDTO validator) {
        MonitoringRoomDO room = new MonitoringRoomDO();
        room.setRoomId(validator.getRoom_id());
        room.setMrName(validator.getMr_name());
        room.setMrState(validator.getMr_state());
        return monitoringRoomMapper.insert(room) > 0;
    }
    @Override
    public boolean deleteById(int id) {
        return monitoringRoomMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateMonitoringRoom(MonitoringRoomDO monitoringRoom, CreateOrUpdateMonitoringRoomDTO validator) {
        monitoringRoom.setRoomId(validator.getRoom_id());
        monitoringRoom.setMrName(validator.getMr_name());
        monitoringRoom.setMrState(validator.getMr_state());
        return monitoringRoomMapper.updateById(monitoringRoom) > 0;
    }
}
